#ifndef CTABLESESSION_H
#define CTABLESESSION_H

#include "AbsTable.h"

struct StSession{
  QString   id;
  QString   name;
  bool      active;
  QString   create_time;
  QString   close_time;
  int       using_time;
  QString   text_code;
  QString   session_type;
  QString   ip;
  int       port;
  QString   seial_name;
  int       baude;
  int       data_bit;
  QString   parity;
  QString   stop_bit;
  QString   flowctrl;
  QString   r_text_url;
  bool      r_is_hex;
  bool      r_show_send;
  bool      r_show_timestamp;
  bool      r_start;
  QString   s_text_url;
  bool      s_is_hex;
  bool      s_auto_enable;
  int       s_auto_span;

  StSession loadData(QVariantList &varList)
  {
      if(varList.count() <= 24){
          qWarning()<<"Size error!"<<varList.count()<<varList;
          return *this;
      }

      int nIndex = 0;
      id               = varList[nIndex++].value<QString>();
      name             = varList[nIndex++].value<QString>();
      active           = varList[nIndex++].value<bool>();
      create_time      = varList[nIndex++].value<QString>();
      close_time       = varList[nIndex++].value<QString>();
      using_time       = varList[nIndex++].value<int>();
      text_code        = varList[nIndex++].value<QString>();
      session_type     = varList[nIndex++].value<QString>();
      ip               = varList[nIndex++].value<QString>();
      port             = varList[nIndex++].value<int>();
      seial_name       = varList[nIndex++].value<QString>();
      baude            = varList[nIndex++].value<int>();
      data_bit         = varList[nIndex++].value<int>();
      parity           = varList[nIndex++].value<QString>();
      stop_bit         = varList[nIndex++].value<QString>();
      flowctrl         = varList[nIndex++].value<QString>();
      r_text_url       = varList[nIndex++].value<QString>();
      r_is_hex         = varList[nIndex++].value<bool>();
      r_show_send      = varList[nIndex++].value<bool>();
      r_show_timestamp = varList[nIndex++].value<bool>();
      r_start          = varList[nIndex++].value<bool>();
      s_text_url       = varList[nIndex++].value<QString>();
      s_is_hex         = varList[nIndex++].value<bool>();
      s_auto_enable    = varList[nIndex++].value<bool>();
      s_auto_span	   = varList[nIndex++].value<int>();

      return *this;
  }

  friend inline QDebug operator<<(QDebug &out, const StSession& info)
  {
      out <<" StSession("<<info.id<<info.name<<") ";
      return out;
  }
};

class CTableSession : public AbsTable
{
    Q_OBJECT
public:
    explicit CTableSession(QObject *parent = nullptr);

    static CTableSession* instance();

    enum Column{
        id = 0,
        name,
        active,
        create_time,
        close_time,
        using_time,
        text_code,
        session_type,
        ip,
        port,
        seial_name,
        baude,
        data_bit,
        parity,
        stop_bit,
        flowctrl,
        r_text_url,
        r_is_hex,
        r_show_send,
        r_show_timestamp,
        r_start,
        s_text_url,
        s_is_hex,
        s_auto_enable,
        s_auto_span,
    };

    bool createTable() override;
    bool addRow(StSession &st);
    bool updateRow(StSession &st);
    bool updateColumn(QString strId,Column nCol,QVariant var);
    bool deleteRow(QString strId);
    bool deleteHistory();
    bool deleteAll();
    StSession queryRow(QString strId);
    QVector<StSession> queryAll();
    QVector<StSession> queryHistory();
signals:

};

#endif // CTABLESESSION_H
